package ru.ifmo.thesis.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Helper {

	private static final String inputFile = "C:/Documents and Settings/Paul/workspace/GeneticAlgorithms/iterative_evalanche_stats_b.txt";
	
	public static void main(String[] args) throws Exception {
		Map<String, Integer> stats = new HashMap<String, Integer>();
		
		Writer results = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream("helper_stats-b.txt")));
		
		StringTokenizer tokenizer = new StringTokenizer(input(), ",");
		while (tokenizer.hasMoreElements()) {
			String token = tokenizer.nextToken();
			Integer count = stats.get(token);
			if (count == null) {
				stats.put(token, Integer.valueOf(1));
			} else {
				stats.put(token, count + 1);
			}
		}
		
		for (Map.Entry<String, Integer> entry : stats.entrySet()) {
			results.write(entry.getKey() + "," + entry.getValue() + "\n");
		}

		results.close();
	}

	private static String input() throws Exception {
		StringBuilder builder = new StringBuilder();
		Reader r = new BufferedReader(new FileReader(inputFile));
		char[] buff = new char[2048];
		int road = 0;
		while ((road = r.read(buff)) > 0) {
			builder.append(buff, 0, road);
		}
		return builder.toString();
	}

}
